import { getQuestionsList, addQuestionsList, deleteQuestionsList } from '../../api/audit/index';

const state = () => {
    return {
        questionsList: []
    }
}

const mutations = {
    SET_LIST(state, data) {
        state.questionsList = data
    }
}

const actions = {
    async getQuestionsList({ commit }) {
        let { data } = await getQuestionsList()
        let form = data.map(v => {
            return { ...v, date: v.date.split(" ")[0] }
        })
        commit("SET_LIST", form)
    },
    async addQuestions({ dispatch, rootState }, form) {
        let data = { ...form, answers: JSON.stringify(form.answers), options: JSON.stringify(form.options), uid: rootState.users.userInfo.id, created: new Date().toLocaleDateString() }
        await addQuestionsList(data)
        dispatch("getQuestionsList")
    },
    async deleteQuestions({ dispatch }, id) {
        await deleteQuestionsList(id)
        dispatch("getQuestionsList")
    }
}

const getters = {

}


export {
    state,
    mutations,
    actions,
    getters
}